<?php

class Application_Model_Rest
{
	private static $table = "t_user";
	private static $cols = array('id'=>'userid', 'password'=>'pwd');

	// private static $rest_url = 'http://rest.rrcc.localhost/';//dev.rest.renrencaichang.com
//	private static $rest_url = 'http://rest.dev.renrencaichang.com/';
//	private static $rest_url = 'http://rest.rrcc.localhost/';
	//private static $rest_url = 'http://rest.test.renrencaichang.com/';
	private static $rest_url = 'http://rest.renrencaichang.com/';
	public function __construct () {
	}

	private function _get ($url) {
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_HEADER, 1);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。
		$data = curl_exec($curl);
		if (curl_getinfo($curl, CURLINFO_HTTP_CODE) == '200') {
			$headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
			$header = substr($data, 0, $headerSize);
			$body = substr($data, $headerSize);
			curl_close($curl);
			return json_decode($body);
		}
		curl_close($curl);
		return null;
	}

	private function _post ($url, $post) {
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_HEADER, 1);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($curl, CURLOPT_POST, 1);  
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。
		curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
		$data = curl_exec($curl);
		if (curl_getinfo($curl, CURLINFO_HTTP_CODE) == '200') {
			$headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
			$header = substr($data, 0, $headerSize);
			$body = substr($data, $headerSize);
			curl_close($curl);
			return json_decode($body);
		}
		curl_close($curl);
		return null;
	}
	private function _put ($url, $post) {
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_HEADER, 1);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。
		curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
		$data = curl_exec($curl);
		if (curl_getinfo($curl, CURLINFO_HTTP_CODE) == '200') {
			$headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
			$header = substr($data, 0, $headerSize);
			$body = substr($data, $headerSize);
			curl_close($curl);
			return json_decode($body);
		}
		curl_close($curl);
		return null; 
	}

	private function _delete ($url, $post) {
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_HEADER, 1);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE' );
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。
		curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
		$data = curl_exec($curl);
		if (curl_getinfo($curl, CURLINFO_HTTP_CODE) == '200') {
			$headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
			$header = substr($data, 0, $headerSize);
			$body = substr($data, $headerSize);
			curl_close($curl);
			return json_decode($body);
		}
		curl_close($curl);
		return null;
	}

	private function _useAuthRequest ($uri,$id,$payload) {
		$auth = new Application_Model_Auth();
		$privateKey = $auth->getPrivateKey();
		$uid = $auth->getUserId();
		$ts = time();
		$data = $privateKey.$id.$ts.$payload;
		return $uri.'&t='.$ts.'&k='.MD5($data).'&uid='.$uid;
	}
	//get
	public function getQAndA(){
		$url = self::$rest_url.'QAndA';
		$obj = $this->_get($url);
		if ($obj != null && $obj->Success == 1)
			return $obj->CallInfo;
		return array();
	}
	public function getAllProvince(){
		$url = self::$rest_url.'ProvinceCity?action=getProvinces';
		$obj = $this->_get($url);
		if ($obj != null && $obj->Success == 1)
			return $obj->CallInfo;
		return array();
	}
	public function getCityByProvinceId($pid){
		$url = self::$rest_url.'ProvinceCity/'.$pid.'?action=getCity';
		$obj = $this->_get($url);
		if ($obj != null && $obj->Success == 1)
			return $obj->CallInfo;
		return array();
	}
	
	public function getAreaByCityId($cid){
		$url = self::$rest_url.'ProvinceCity?action=getArea&cityid='.$cid;
		$obj = $this->_get($url);
		if ($obj != null && $obj->Success == 1)
			return $obj->CallInfo;
		return array();
	}
	
	public function getCityDistrictList($cityid){
		if (empty($cityid))
			return array();
		$url = self::$rest_url.'CityDistrictList?cityid='.$cityid;
		$obj = $this->_get($url);
		if ($obj != null && $obj->Success == 1)
			return $obj->CallInfo;
		return array();
	}
	public function getUserProductDetails($skuid){
		if (empty($skuid))
			return array();
		$url = self::$rest_url.'UserProductDetails/'.$skuid;
		$obj = $this->_get($url);
		if ($obj != null && $obj->Success == 1)
			return $obj->CallInfo;
		return array();
	}
	public function getUserPresaleAddressList($uid,$channelId){
		if(empty($uid) || empty($channelId)){
			return array();
		}
		$url = self::$rest_url.'UserPresaleAddressList/?uid='.$uid.'&channelId='.$channelId;
		$url = $this->_useAuthRequest($url,'','');
		$obj = $this->_get($url);
		if ($obj != null && $obj->Success == 1)
			return $obj->CallInfo;
		return array();
	}
	public function getUserPresaleProductDetails($skuid){
		if (empty($skuid))
			return array();
		$url = self::$rest_url.'UserPresaleProductDetails/'.$skuid;
		$obj = $this->_get($url);
		if ($obj != null && $obj->Success == 1)
			return $obj->CallInfo;
		return array();
	}

	public function getDistrictSupplierList($districtId){
		if (empty($districtId))
			return array();
		$url = self::$rest_url.'DistrictSupplierList?districtId='.$districtId;
		$obj = $this->_get($url);
		if ($obj != null && $obj->Success == 1)
			return $obj->CallInfo;
		return array();
	}
	public function getDistributerList($sid){
		if (empty($sid))
			return array();
		$url = self::$rest_url.'DistributerList?sid='.$sid;
		$obj = $this->_get($url);
		if ($obj != null && $obj->Success == 1)
			return $obj->CallInfo;
		return array();
	}
	public function getRShopList($openid){
		if (empty($openid))
			return array();
		$url = self::$rest_url.'RshopList?oid='.$openid;
		$obj = $this->_get($url);
		if ($obj != null && $obj->Success == 1)
			return $obj->CallInfo;
		return array();
	}
	public function getUserOrderTransaction($transactionId){
		$url = self::$rest_url.'UserOrderTransaction/'.$transactionId;
		$obj = $this->_get($url);
		if ($obj != null && $obj->Success == 1)
			return $obj->CallInfo;
		return array();
	}
	 public function getShopDetails($shopid){
		$url = self::$rest_url.'ShopDetails/'.$shopid;
		$obj = $this->_get($url);
		if ($obj != null && $obj->Success == 1)
			return $obj->CallInfo;
		return array();
	}
	public function getUserTopup($topupid){
		$url = self::$rest_url.'UserTopup?topupid='.$topupid;
		$url = $this->_useAuthRequest($url,'','');
		$obj = $this->_get($url);
		// if($obj->ErrorCode == 2002){
			// $auth = new Application_Model_Auth();
			// $auth->logout();
			// $rurl = '/';
			// header('Location:/login?rurl='.$rurl);
		// }
		if ($obj != null && $obj->Success == 1)
			return $obj->CallInfo;
		return array();
	}
	public function getUser(){
		$url = self::$rest_url.'User?';
		$url = $this->_useAuthRequest($url,'','');
		$obj = $this->_get($url);
		if($obj->ErrorCode == 2002){
			$auth = new Application_Model_Auth();
			$auth->logout();
			$rurl = '/';
			header('Location:/login?rurl='.$rurl);
		}
		if ($obj != null && $obj->Success == 1)
			return $obj->CallInfo;
		return array();
	}
	public function getUserCartDetails($cartList){
		$url = self::$rest_url.'UserCartDetails?cartList='.$cartList;
		$url = $this->_useAuthRequest($url,'','');
		$obj = $this->_get($url);
		if($obj->ErrorCode == 2002){
			$auth = new Application_Model_Auth();
			$auth->logout();
			$rurl = '/';
			header('Location:/login?rurl='.$rurl);
		}
		if ($obj != null && $obj->Success == 1)
			return $obj->CallInfo;
		return array();
	}
	public function getUserTopupRole($roleid){
		$url = self::$rest_url.'UserTopupRole?roleid='.$roleid;
		$obj = $this->_get($url);
		if ($obj != null && $obj->Success == 1)
			return $obj->CallInfo;
		return array();
	}
	public function getUserCreditRole($roleid){
		$url = self::$rest_url.'UserCreditRole?roleid='.$roleid;
		$url = $this->_useAuthRequest($url,'','');
		$obj = $this->_get($url);
		if($obj->ErrorCode == 2002){
			$auth = new Application_Model_Auth();
			$auth->logout();
			$rurl = '/';
			header('Location:/login?rurl='.$rurl);
		}
		if ($obj != null && $obj->Success == 1)
			return $obj->CallInfo;
		return array();
	}
	public function getUserOrderPreSubmit($id){
		if (empty($id))
			return array();
		$url = self::$rest_url.'UserOrderPreSubmit/'.$id.'?';
		$url = $this->_useAuthRequest($url,$id,'');
		$obj = $this->_get($url);
		if($obj->ErrorCode == 2002){
			$auth = new Application_Model_Auth();
			$auth->logout();
			$rurl = '/order/list';
			header('Location:/login?rurl='.$rurl);
		}
		if ($obj != null && $obj->Success == 1)
			return $obj->CallInfo;
		return array();
	}
	
	public function getUserOrderList($id){
		if (empty($id))
			return array();
		$url = self::$rest_url.'UserOrderList/'.$id.'?';
		$url = $this->_useAuthRequest($url,$id,'');
		$obj = $this->_get($url);
		if($obj->ErrorCode == 2002){
			$auth = new Application_Model_Auth();
			$auth->logout();
			$rurl = '/order/list';
			header('Location:/login?rurl='.$rurl);
		}
		if ($obj != null && $obj->Success == 1)
			return $obj->CallInfo;
		return array();
	}

	public function getShopAppraiseList($shopid){
		$url = self::$rest_url.'ShopAppraiseList/'.$shopid.'?orderId=0&o=0&l=10';
		$obj = $this->_get($url);
		if ($obj != null && $obj->Success == 1)
			return $obj->CallInfo;
		return array();
	}
	
	public function getRshopPromotionList($shopid){
		$url = self::$rest_url.'RshopPromotionList?sid='.$shopid;
		$obj = $this->_get($url);
		if ($obj != null && $obj->Success == 1)
			return $obj->CallInfo;
		return array();
	}
	
	public function getUserShopProductList($shopid){
		$url = self::$rest_url.'UserShopProductList?sid='.$shopid;
		$obj = $this->_get($url);
		if ($obj != null && $obj->Success == 1)
			return $obj->CallInfo;
		return array();
	}

	public function getUserPresaleShopProductList($cityId,$siteId){
		$url = self::$rest_url.'UserPresaleShopProductList?cityId='.$cityId.'&siteId='.$siteId;
		$obj = $this->_get($url);
		if ($obj != null && $obj->Success == 1)
			return $obj->CallInfo;
		return array();
	}
	
	
	public function getUserOrderDetails($id){
		$url = self::$rest_url.'UserOrderDetails/'.$id.'?';
		$url = $this->_useAuthRequest($url,$id,'');
		$obj = $this->_get($url);
		if($obj->ErrorCode == 2002){
			$auth = new Application_Model_Auth();
			$auth->logout();
			$rurl = '/order/list';
			header('Location:/login?rurl='.$rurl);
		}
		if ($obj != null && $obj->Success == 1)
			return $obj->CallInfo;
		return array();
	}   
	public function getVoucherList(){
		$url = self::$rest_url.'UserVoucherList?';
		$url = $this->_useAuthRequest($url,'','');
		$obj = $this->_get($url);
		if($obj->ErrorCode == 2002){
			$auth = new Application_Model_Auth();
			$auth->logout();
			$rurl = '/wealfare';
			header('Location:/login?rurl='.$rurl);
		}
		if ($obj != null && $obj->Success == 1)
			return $obj->CallInfo;
		return array();
	}
	public function getSiteList($cityId,$name,$lat,$lon){
		$url = self::$rest_url.'SiteList?cityId='.$cityId.'&name='.$name.'&lat='.$lat.'&lon='.$lon;
		$obj = $this->_get($url);
		if ($obj != null && $obj->Success == 1)
			return $obj->CallInfo;
		return array();
	}
	public function getVoucherDetailList($ref){
		$url = self::$rest_url.'VoucherDetailList?ref='.$ref;
		$obj = $this->_get($url);
		if ($obj != null && $obj->Success == 1)
			return $obj->CallInfo;
		return array();
	}
	public function getSiteShopList($siteId){
		$url = self::$rest_url.'SiteShopList/'.$siteId;
		$obj = $this->_get($url);
		if ($obj != null && $obj->Success == 1)
			return $obj->CallInfo;
		return array();
	}
	public function getDistributerActive($did){
		$url = self::$rest_url.'DistributerActive?distributerid='.$did;
		$obj = $this->_get($url);
		if ($obj != null && $obj->Success == 1)
			return $obj->CallInfo;
		return array();
	}
	
	public function getShopProductByShopIdAndProductId($shopId,$productId){
		$url = self::$rest_url.'ShopProductByShopIdAndProductId?action=ShopProductByShopIdAndProductId&shop_id='.$shopId.'&product_id='.$productId;
		$obj = $this->_get($url);
		if ($obj != null && $obj->Success == 1)
			return $obj->CallInfo;
		return array();
	}
	
	public function getSiteMarket($siteId){
		$url = self::$rest_url.'SiteMarket/'.$siteId;
		$obj = $this->_get($url);
		if ($obj != null && $obj->Success == 1)
			return $obj->CallInfo;
		return array();
	}

	public function getUserVoucherList(){
		$url = self::$rest_url.'UserVoucherList?';
		$url = $this->_useAuthRequest($url,'','');
		$obj = $this->_get($url);
		if($obj->ErrorCode == 2002){
			$auth = new Application_Model_Auth();
			$auth->logout();
			$rurl = '/wealfare';
			header('Location:/login?rurl='.$rurl);
		}
		if ($obj != null && $obj->Success == 1)
			return $obj->CallInfo;
		return array();
	}

	//post
	public function postUserPresaleAddress($data,$uid){
		$url = self::$rest_url.'UserPresaleAddress?uid='.$uid;
		$payload = 'cityId='.$data["cityId"].'&isDefaultAds='.$data["isDefaultAds"].'&tel='.$data["tel"].'&contacts='.$data["contacts"].'&address='.$data["address"].'&channelId='.$data["channelId"].'&siteId='.$data["siteId"].'';
		$payload = base64_encode($payload);
		$post = 'payload='.$payload;
		$url = $this->_useAuthRequest($url,'',$payload);
		$obj = $this->_post($url,$post);
		if ($obj != null && $obj->Success == 1)
			return $obj->CallInfo;
		return null;
	}
	public function postPartner($data){
		$url = self::$rest_url.'Partner?';
		$payload = 'account='.$data["account"].'&sex='.$data["sex"].'&idCartNum='.$data["idCartNum"].'&tel='.$data["tel"].'&partnerMethod='.$data["partnerMethod"].'&money='.$data["money"].'&place='.$data["place"].'';
		$payload = base64_encode($payload);
		$post = 'payload='.$payload;
		$obj = $this->_post($url,$post);
		if ($obj != null && $obj->Success == 1)
			return $obj->Success;
		return null;
	}
	public function postForwardingVoucher($mobile,$ref,$openId,$nickname,$headimgurl){
		$url = self::$rest_url.'VoucherIssuer?';
		$payload = 'action=forwarding&mobile='.$mobile.'&ref='.$ref.'&openId='.$openId.'&nickname='.$nickname.'&avatar='.$headimgurl;
		$obj = $this->_post($url,$payload);
		if ($obj != null && $obj->Success == 1)
			return $obj->CallInfo;
		return null;
	}
	 public function postUserCreditConsume($info){
		$url = self::$rest_url.'UserCreditConsume?';
		$payload = 'giftid='.$info["giftid"].'&gift_credit='.$info["gift_credit"].'&gift='.$info["gift"].'&gift_title='.$info["gift_title"].'&gift_limit='.$info["gift_limit"].'&gift_starttime='.$info["gift_starttime"].'&gift_expired='.$info["gift_expired"];
		$payload = base64_encode($payload);
		$post = 'payload='.$payload;
		$url = $this->_useAuthRequest($url,'',$payload);
		$obj = $this->_post($url,$post);
		if(!empty($obj)){
			if($obj->ErrorCode == 2002){
				$auth = new Application_Model_Auth();
				$auth->logout();
				$rurl = '/';
				header('Location:/login?rurl='.$rurl);
			}
		}
		if ($obj != null && $obj->Success == 1)
			return $obj->CallInfo;
		return array();
	 }
	 //topup
	 public function postUserTopup($type,$expenditure,$title,$referid){
		$url = self::$rest_url.'UserTopup?';
		$payload = 'type='.$type.'&expenditure='.$expenditure.'&title='.$title.'&referid='.$referid;
		$payload = base64_encode($payload);
		$post = 'payload='.$payload;
		$url = $this->_useAuthRequest($url,'',$payload);
		$obj = $this->_post($url,$post);
		if(!empty($obj)){
			if($obj->ErrorCode == 2002){
				$auth = new Application_Model_Auth();
				$auth->logout();
				$rurl = '/';
				header('Location:/login?rurl='.$rurl);
			}
		}
		if ($obj != null && $obj->Success == 1)
			return $obj->CallInfo;
		return array();
	 }
	 public function postUserOrder($siteId,$orderList,$channel,$sale_channel=1000){
		//$sale_channel = 1003;
		$url = self::$rest_url.'UserOrder?sale_channel='.$sale_channel;
		$payload = 'siteId='.$siteId.'&orderList='.$orderList.'&channel='.$channel;
		$payload = base64_encode($payload);
		$post = 'payload='.$payload;
		$url = $this->_useAuthRequest($url,'',$payload);
		$obj = $this->_post($url,$post);
		 if(!empty($obj)){
			if($obj->ErrorCode == 2002){
				$auth = new Application_Model_Auth();
				$auth->logout();
				$rurl = '/site';
				header('Location:/login?rurl='.$rurl);
			}
		}
		if ($obj != null && $obj->Success == 1)
			return $obj->CallInfo;
		return array();
	 }
	public function postUserAppraise($data){
		$url = self::$rest_url.'UserAppraise/?';
		$payload = 'freshScore='.$data["freshScore"].'&deliveryScore='.$data["deliveryScore"].'&priceScore='.$data["priceScore"].'&serviceScore='.$data["serviceScore"].'&colligateScore='.$data["colligateScore"].'&content='.$data["content"].'&orderId='.$data["orderId"].'&clientId='.$data["clientId"];
		$payload = base64_encode($payload);
		$post = 'payload='.$payload;
		$url = $this->_useAuthRequest($url,'',$payload);	
		$obj = $this->_post($url,$post);
		if($obj->ErrorCode == 2002){
			$auth = new Application_Model_Auth();
			$auth->logout();
			$rurl = '/order/list';
			header('Location:/login?rurl='.$rurl);
		}
		if ($obj != null && $obj->Success == 1)
			return $obj->Success;
		return array();
	}
		
	 public function addUserSharingVoucher($mobile){
		$url = self::$rest_url.'VoucherIssuer';
		$post = 'mobile='.$mobile.'&action=sharing';
		return $this->_post($url,$post);
	}

	public function getCaptcha ($mobile) {
		if (empty($mobile))
			return array();
		$post = 'mobile='.$mobile.'&device=rshop';
		$url = self::$rest_url.'UserCaptcha';
		$obj = $this->_post($url,$post);
		if ($obj != null && $obj->Success == 1)
			return array($obj->CallInfo);
		return array();
	}
	//put
	public function putUserOrder($action,$id){
		$url = self::$rest_url.'UserOrder/'.$id.'?';
		$payload = 'action='.$action;
		$payload = base64_encode($payload);
		$url = $this->_useAuthRequest($url,$id,$payload);
		$post = 'payload='.$payload;
		$obj = $this->_put($url,$post);
		if(array_key_exists('ErrorCode',$obj)){
			if($obj->ErrorCode == 2002){
				$auth = new Application_Model_Auth();
				$auth->logout();
				return false;
			}
		}
		if ($obj != null && $obj->Success == 1)
			return $obj->CallInfo;
		return false;
	}
	public function putUserTopup($action,$id){
		$url = self::$rest_url.'UserTopup/'.$id.'?';
		$payload = 'action='.$action;
		$payload = base64_encode($payload);
		$url = $this->_useAuthRequest($url,$id,$payload);
		$post = 'payload='.$payload;
		$obj = $this->_put($url,$post);
		if ($obj != null && $obj->Success == 1)
			return $obj->CallInfo;
		return false;
	}
	
	public function putCaptcha ($mobile,$captcha) {
		if (empty($mobile))
			return array();
		$post = 'mobile='.$mobile.'&captcha='.$captcha.'&device=rshop';
		$url = self::$rest_url.'UserCaptcha';
		$obj = $this->_put ($url, $post);
		if ($obj != null && $obj->Success == 1)
			return $obj->CallInfo;
		return array();
	}
	// public function putUserOrderTransaction($transactionId,$trade_no){
		// if(empty($transactionId)){
			// return array();
		// }
		// $payload = 'trade_no='.$trade_no;
		// $payload = base64_encode($payload);
		// $post = 'payload='.$payload;
		// $url = self::$rest_url.'UserOrderTransaction/'.$transactionId.'?';
		// $obj = $this->_put($url,$post);
		// if ($obj != null && $obj->Success == 1)
			// return $obj->CallInfo;
		// return array();
	// }
	public function putUserOrderTransaction($transactionId,$trade_no,$isSuccess){
		if(empty($transactionId)){
			return array();
		}
		$payload = 'trade_no='.$trade_no.'&isSuccess='.$isSuccess;
		$payload = base64_encode($payload);
		$post = 'payload='.$payload;
		$url = self::$rest_url.'UserOrderTransaction/'.$transactionId.'?';
		$obj = $this->_put($url,$post);
		if ($obj != null && $obj->Success == 1)
			return $obj->CallInfo;
		return array();
	}
}
